package com.fjxd.order.component;


import com.fjxd.order.pojo.Order;
import com.fjxd.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

import static com.fjxd.order.service.impl.OrderServiceImpl.PASTDUE;
import static com.fjxd.order.service.impl.OrderServiceImpl.UNPAID;

/**
 * @author csw
 */
@Component
public class RedisSubService extends KeyExpirationEventMessageListener {

    public RedisSubService(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    @Autowired
    OrderService orderService;


    @Override
    public void onMessage(Message message, byte[] pattern) {
        super.onMessage(message, pattern);
        try{
            String key = message.toString();
            if(key!=null&& key.startsWith("order_")){
                String orderNo = key.substring(6);
                Order byId = orderService.getById(orderNo);
                if(byId.getOrderStatus()==UNPAID){
                    byId.setOrderStatus(PASTDUE);
                    orderService.updateOrder(byId);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
